package com.ruoyi.analysiswarning.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.analysiswarning.domain.AnalyOverview;
import com.ruoyi.analysiswarning.domain.vo.OverViewVo;
import com.ruoyi.analysiswarning.domain.vo.PageVO;
import com.ruoyi.analysiswarning.domain.vo.QueryVo;
import com.ruoyi.analysiswarning.mapper.AnalysiswarningMapper;
import com.ruoyi.analysiswarning.service.IAnalysiswarningService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AnalysiswarningService implements IAnalysiswarningService {

    public static  List<String> titleValues0=new ArrayList<>();
    public static  List<String> titleValues1=new ArrayList<>();
    public static  List<String> titleValues2=new ArrayList<>();
    public static  List<String> titleValues3=new ArrayList<>();
    public static  List<String> titleValues4=new ArrayList<>();
    public static  List<String> titleValues5=new ArrayList<>();
    @Resource
    AnalysiswarningMapper analysiswarningMapper;

    {
        handTitleValues(titleValues0);
        handTitleValues(titleValues1);
        handTitleValues(titleValues2);
        handTitleValues(titleValues3);
        handTitleValues(titleValues4);
        handTitleValues(titleValues5);
    }

    public void handTitleValues(List<String> titleValues){

        while (true) {
            double num = Math.random() * 100;

            if (num<70.0){
                continue;
            }

            if (titleValues.size()<=100){
                titleValues.add(String.format("%2.2f",num));
            }else {
                break;
            }

        }
    }

    @Override
    public Map queryEcharts(QueryVo queryVo) {
        // 查询4个echarts
        queryVo.setColumn("analy_source");
        List<Map> list1 = analysiswarningMapper.queryEcharts(queryVo);
        HashMap map = new HashMap();
        map.put("echart0",list1);
        queryVo.setColumn("analy_level");
        List<Map> list2 = analysiswarningMapper.queryEcharts(queryVo);
        map.put("echart1",list2);
        queryVo.setColumn("obj_name");
        List<Map> list3 = analysiswarningMapper.queryEcharts(queryVo);
        map.put("echart2",list3);


        List<AnalyOverview> data = this.queryList(new OverViewVo().setPageNumber(1).setPageSize(0).setTbId(1).setObj(queryVo.getAppsystem())).getData();
        List<String> titles = data.stream().map(AnalyOverview::getObjName).distinct().collect(Collectors.toList());

        List<Map<String, String>> echarts3 = new ArrayList<>();
        List<Map<String, String>> echarts4 = new ArrayList<>();
        List<Map<String, String>> echarts5 = new ArrayList<>();
        List<Map<String, String>> echarts6 = new ArrayList<>();
        List<Map<String, String>> echarts7 = new ArrayList<>();
        List<Map<String, String>> echarts8 = new ArrayList<>();

        for (int i = 0; i < titles.size(); i++) {
            Map<String, String> map3 = new HashMap<>();
            map3.put("x",titles.get(i));
            map3.put("y",titleValues0.get(i));
            echarts3.add(map3);



            if (StringUtils.isNotEmpty(queryVo.getOrg())){
                Double s4 = Double.parseDouble(titleValues1.get(i));
                Double s5 = Double.parseDouble(titleValues2.get(i));
                Double s6 = Double.parseDouble(titleValues3.get(i));
                Double s7 = Double.parseDouble(titleValues4.get(i));
                Double s8 = Double.parseDouble(titleValues5.get(i));
                int dou = Integer.parseInt(queryVo.getOrg());

                if (dou == 1){
                    double doo = 70;
                    if (doo >= s4){
                        Map<String, String> map4 = new HashMap<>();
                        map4.put("x",titles.get(i));
                        map4.put("y",s4.toString());
                        echarts4.add(map4);
                    }

                    if (doo >= s5){
                        Map<String, String> map5 = new HashMap<>();
                        map5.put("x",titles.get(i));
                        map5.put("y",s5.toString());
                        echarts5.add(map5);
                    }

                    if (doo >= s6){
                        Map<String, String> map6 = new HashMap<>();
                        map6.put("x",titles.get(i));
                        map6.put("y",s6.toString());
                        echarts6.add(map6);
                    }


                    if (doo >= s7){
                        Map<String, String> map7 = new HashMap<>();
                        map7.put("x",titles.get(i));
                        map7.put("y",s7.toString());
                        echarts7.add(map7);
                    }


                    if (doo >= s8){
                        Map<String, String> map8 = new HashMap<>();
                        map8.put("x",titles.get(i));
                        map8.put("y",s8.toString());
                        echarts8.add(map8);
                    }
                }
                else if (dou == 2){
                    double doo = 80;
                    double dooo = 70;
                    if (dooo <= s4 && s4<= doo){
                        Map<String, String> map4 = new HashMap<>();
                        map4.put("x",titles.get(i));
                        map4.put("y",s4.toString());
                        echarts4.add(map4);
                    }

                    if (dooo <= s5 && s5<= doo){
                        Map<String, String> map5 = new HashMap<>();
                        map5.put("x",titles.get(i));
                        map5.put("y",s5.toString());
                        echarts5.add(map5);
                    }

                    if (dooo <= s6 && s6<= doo){
                        Map<String, String> map6 = new HashMap<>();
                        map6.put("x",titles.get(i));
                        map6.put("y",s6.toString());
                        echarts6.add(map6);
                    }


                    if (dooo <= s7 && s7<= doo){
                        Map<String, String> map7 = new HashMap<>();
                        map7.put("x",titles.get(i));
                        map7.put("y",s7.toString());
                        echarts7.add(map7);
                    }


                    if (dooo <= s8 && s8<= doo){
                        Map<String, String> map8 = new HashMap<>();
                        map8.put("x",titles.get(i));
                        map8.put("y",s8.toString());
                        echarts8.add(map8);
                    }
                }
                else if (dou == 3){
                    double doo = 90;
                    double dooo = 80;
                    if (dooo <= s4 && s4<= doo){
                        Map<String, String> map4 = new HashMap<>();
                        map4.put("x",titles.get(i));
                        map4.put("y",s4.toString());
                        echarts4.add(map4);
                    }

                    if (dooo <= s5 && s5<= doo){
                        Map<String, String> map5 = new HashMap<>();
                        map5.put("x",titles.get(i));
                        map5.put("y",s5.toString());
                        echarts5.add(map5);
                    }

                    if (dooo <= s6 && s6<= doo){
                        Map<String, String> map6 = new HashMap<>();
                        map6.put("x",titles.get(i));
                        map6.put("y",s6.toString());
                        echarts6.add(map6);
                    }


                    if (dooo <= s7 && s7<= doo){
                        Map<String, String> map7 = new HashMap<>();
                        map7.put("x",titles.get(i));
                        map7.put("y",s7.toString());
                        echarts7.add(map7);
                    }


                    if (dooo <= s8 && s8<= doo){
                        Map<String, String> map8 = new HashMap<>();
                        map8.put("x",titles.get(i));
                        map8.put("y",s8.toString());
                        echarts8.add(map8);
                    }
                }
                else if (dou == 4){
                    double doo = 90;
                    if (doo <= s4){
                        Map<String, String> map4 = new HashMap<>();
                        map4.put("x",titles.get(i));
                        map4.put("y",s4.toString());
                        echarts4.add(map4);
                    }

                    if (doo <= s5){
                        Map<String, String> map5 = new HashMap<>();
                        map5.put("x",titles.get(i));
                        map5.put("y",s5.toString());
                        echarts5.add(map5);
                    }

                    if (doo <= s6){
                        Map<String, String> map6 = new HashMap<>();
                        map6.put("x",titles.get(i));
                        map6.put("y",s6.toString());
                        echarts6.add(map6);
                    }


                    if (doo <= s7){
                        Map<String, String> map7 = new HashMap<>();
                        map7.put("x",titles.get(i));
                        map7.put("y",s7.toString());
                        echarts7.add(map7);
                    }

                    if (doo <= s7){
                        Map<String, String> map8 = new HashMap<>();
                        map8.put("x",titles.get(i));
                        map8.put("y",s8.toString());
                        echarts8.add(map8);
                    }

                }

            }
            else{

                Map<String, String> map4 = new HashMap<>();
                map4.put("x",titles.get(i));
                map4.put("y",titleValues1.get(i));
                echarts4.add(map4);

                Map<String, String> map5 = new HashMap<>();
                map5.put("x",titles.get(i));
                map5.put("y",titleValues2.get(i));
                echarts5.add(map5);

                Map<String, String> map6 = new HashMap<>();
                map6.put("x",titles.get(i));
                map6.put("y",titleValues3.get(i));
                echarts6.add(map6);


                Map<String, String> map7 = new HashMap<>();
                map7.put("x",titles.get(i));
                map7.put("y",titleValues4.get(i));
                echarts7.add(map7);


                Map<String, String> map8 = new HashMap<>();
                map8.put("x",titles.get(i));
                map8.put("y",titleValues5.get(i));
                echarts8.add(map8);
            }

        }

        echarts3=echarts3.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());
        echarts4=echarts4.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());
        echarts5=echarts5.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());
        echarts6=echarts6.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());
        echarts7=echarts7.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());
        echarts8=echarts8.stream().sorted((x,y)->Double.valueOf(y.get("y")).compareTo(Double.valueOf(x.get("y")))).collect(Collectors.toList());

        map.put("echarts3",echarts3);
        map.put("echarts4",echarts4);
        map.put("echarts5",echarts5);
        map.put("echarts6",echarts6);
        map.put("echarts7",echarts7);
        map.put("echarts8",echarts8);
        return map;
    }

    @Override
    public PageVO<List<AnalyOverview>> queryList(OverViewVo overViewVo) {
        if (overViewVo.getDate() != null) {
            if (overViewVo.getDate().size() > 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                overViewVo.setStartDate(sdf.format(overViewVo.getDate().get(0)));
                overViewVo.setEndDate(sdf.format(overViewVo.getDate().get(1)));
            }
        }
        PageHelper.startPage(overViewVo.getPageNumber(),overViewVo.getPageSize());

        List<AnalyOverview> list = analysiswarningMapper.queryList(overViewVo);

        PageInfo<AnalyOverview> page = new PageInfo<>(list);

        PageVO<List<AnalyOverview>> analyOverviewPageVO = new PageVO<>();

        analyOverviewPageVO.setPageNumber(page.getPageNum()).setPageSize(page.getPageSize()).setTotal(page.getTotal()).setData(page.getList());

        return analyOverviewPageVO;
    }

    @Override
    public int updateState(String id, String code) {
        analysiswarningMapper.updateState(id,code);
        return 1;
    }

    @Override
    public void updateAll(List<AnalyOverview> analyOverviews) {
        for(AnalyOverview analyOverview: analyOverviews){
            analysiswarningMapper.updateById(analyOverview);
        }

    }
}
